<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>WebGL - Clipspace Rectangles with varying</title>
    <link type="text/css" href="../resources/webgl-tutorials.css" rel="stylesheet" />
</head>

<body>
    <div id="info">
        <a href="https://webglfundamentals.org/webgl/lessons/zh_cn/webgl-3d-perspective-correct-texturemapping.html"
            target="_blank">WebGL 纹理映射的透视纠正</a>
    </div>
    <canvas id="c"></canvas>
</body>

</html>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See http://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and http://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<script src="../resources/webgl-utils.js"></script>
<script id="2d-vertex-shader" type="notjs">

    attribute vec4 a_position;
    attribute float a_brightness;

    varying float v_brightness;

    void main() {
        gl_Position = a_position;

        // 直接传递亮度到片断着色器
        v_brightness = a_brightness;
    }

</script>

<script id="2d-fragment-shader" type="notjs">
    precision mediump float;
    // 顶点着色器的值插值后传入这里
    varying float v_brightness;  

    void main() {
        gl_FragColor = vec4(v_brightness, 0, 0, 1);  // 红色
    }

</script>
<script>
    "use strict";

    function main() {
        // Get A WebGL context
        var canvas = document.getElementById("c");
        var gl = canvas.getContext("webgl");
        if (!gl) {
            return;
        }

        // setup GLSL program
        var program = webglUtils.createProgramFromScripts(gl, ["2d-vertex-shader", "2d-fragment-shader"]);

        // 找到顶点数据的输入位置
        var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
        var brightnessAttributeLocation = gl.getAttribLocation(program, "a_brightness");

        // Create a buffer and put 12 clip space points in it.
        // 4 rectangles, 2 triangles each, 3 vertices per triangle
        var positionBuffer = gl.createBuffer();

        // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
        gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

        var positions = [
            -.8, .2, 0, 1,  // 1nd rect 1st triangle
            .8, .2, 0, 1,
            -.8, .8, 0, 1,
            -.8, .8, 0, 1,  // 1nd rect 2nd triangle
            .8, .2, 0, 1,
            .8, .8, 0, 1,

            -.8, -.8, 0, 1,  // 2st rect 1st triangle
            .8, -.8, 0, 1,
            -.8, -.2, 0, 1,
            -.8, -.2, 0, 1,  // 2st rect 2nd triangle
            .8, -.8, 0, 1,
            .8, -.2, 0, 1,
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

        // 创建缓冲并放入 12 个亮度值
        var brightnessBuffer = gl.createBuffer();

        // 绑定到 ARRAY_BUFFER (think of it as ARRAY_BUFFER = brightnessBuffer)
        gl.bindBuffer(gl.ARRAY_BUFFER, brightnessBuffer);

        var brightness = [
            0,  // 第一个矩形的第一个三角形
            1,
            0,
            0,  // 第一个矩形的第二个三角形
            1,
            1,

            0,  // 第二个矩形的第一个三角形
            1,
            0,
            0,  // 第二个矩形的第二个三角形
            1,
            1,
        ];

        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(brightness), gl.STATIC_DRAW);

        // code above this line is initialization code.
        // code below this line is rendering code.

        webglUtils.resizeCanvasToDisplaySize(gl.canvas);

        // Tell WebGL how to convert from clip space to pixels
        gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

        // Clear the canvas
        gl.clearColor(0, 0, 0, 0);
        gl.clear(gl.COLOR_BUFFER_BIT);

        // Tell it to use our program (pair of shaders)
        gl.useProgram(program);

        // 启用属性
        gl.enableVertexAttribArray(positionAttributeLocation);

        // Bind the position buffer.
        gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

        // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
        var size = 4;          // 4 components per iteration
        var type = gl.FLOAT;   // the data is 32bit floats
        var normalize = false; // don't normalize the data
        var stride = 0;        // 0 = move forward size * sizeof(type) each iteration to get the next position
        var offset = 0;        // start at the beginning of the buffer
        gl.vertexAttribPointer(
            positionAttributeLocation, size, type, normalize, stride, offset);

        // 启用属性
        gl.enableVertexAttribArray(brightnessAttributeLocation);

        // 绑定位置缓冲
        gl.bindBuffer(gl.ARRAY_BUFFER, brightnessBuffer);

        // 告诉属性如何从缓冲中读取数据
        var size = 1;          // 每次迭代读取一个单位数据
        var type = gl.FLOAT;   // 数据类型是 32 位浮点型
        var normalize = false; // 不用单位化
        var stride = 0;        // 每次迭代需要移动的距离
        var offset = 0;        // 从缓冲的起始处开始
        gl.vertexAttribPointer(
            brightnessAttributeLocation, size, type, normalize, stride, offset);

        // draw
        var primitiveType = gl.TRIANGLES;
        var offset = 0;
        var count = 4 * 3;   // 4 triangles, 3 vertices each
        gl.drawArrays(primitiveType, offset, count);
    }

    main();
</script>